<?php
namespace AuctionCore\Other;

use AuctionCore\System\AuctionException;

/**
 * 消息功能类
 * @author angl
 *
 */
class Inbox
{

    private $model = null;

    public function __construct()
    {
        $this->model = M('inbox');
    }


    public static function sendMessage($uid, $title, $content, $from = 0, $type = 1)
    {
        if ((int)$uid < 1 || empty($title) || empty($content)) {
            return false;
        }
        $data = array(
            'uid' => $uid,
            'title' => $title,
            'content' => $content,
            'from' => $from,
            'type' => $type,
            'is_read' => 0,
            'create_time' => date("Y-m-d H:i:s")
        );

        return M('inbox')->add($data);
    }

    /**
     * 获取列表
     * @param $page
     * @param $limit
     * @param $params
     * @return array
     */
    public static function getList($page, $limit, $params)
    {
        $page = intval($page) < 1 ? 1 : intval($page);
        $limit = intval($limit) < 1 ? 10 : intval($limit);

        $cols = "id,title,content,type,is_read,create_time";
        $map = array();
        $map['uid'] = $params['uid'];
        if ((int)$params['type'] > 0) {
            $map['type'] = array('eq', (int)$params['type']);
        }
        $total = M('inbox')->where($map)->count();
        $list = M('inbox')->field($cols)->where($map)->page($page)->order("create_time desc")->limit($limit)->select();
        $result = array(
            'currentpage' => $page,
            'pagesize' => $limit,
            'maxcount' => $total,
            'maxpage' => ceil($total / $limit),
            'list' => $list
        );
        return $result;
    }

    /**
     * 获取未读消息数目
     * @param $uid
     * @param int $type
     * @return bool
     */
    public static function getUnreadCount($uid, $type = 0)
    {
        if ((int)$uid < 1) {
            return false;
        }
        $map = array();
        $map['uid'] = $uid;
        $map['is_read'] = 0;
        if ($type > 0) {
            $map['type'] = $type;
        }
        return M('inbox')->where($map)->count();
    }

    /**
     * 获取消息详情
     * @param $id
     * @return bool|mixed
     */
    public static function getDetailById($id)
    {
        if ((int)$id < 1) {
            return false;
        }
        $map = array(
            'id' => $id
        );
        return M('inbox')->where($map)->find();
    }

    /**
     * 标记为已读
     * @param $id
     * @return bool
     */
    public static function make_as_read($id)
    {
        if ((int)$id < 1) {
            return false;
        }
        $map = array('id' => $id);
        $data = array('is_read' => 1);
        return M('inbox')->where($map)->save($data);
    }

    /**
     * 标记全部为已读
     * @param $uid
     * @return bool
     */
    public static function make_all_as_read($uid)
    {
        if ((int)$uid < 1) {
            return false;
        }
        $map = array(
            'uid' => array('eq', $uid),
            'is_read' => array('eq', 0)
        );
        $data = array('is_read' => 1);
        return M('inbox')->where($map)->save($data);
    }

    /**
     * 删除消息
     * @param $mid
     * @return mixed
     */
    public function deleteMessage($mid)
    {
        return $this->model->where("id={$mid}")->delete();
    }
}